package com.xh.demo.utils;

import java.io.*;
import java.nio.channels.FileChannel;

/**
 * @Name FileMergeUtils
 * @Description
 * @Author wen
 * @Date 2020-06-08
 */
public class FileMergeUtils {

    //    public static boolean mergeFilesNio(String[] fpaths, String resultPath) {
//        if (fpaths == null || fpaths.length < 1) {
//            return false;
//        }
//        if (fpaths.length == 1) {
//            return new File(fpaths[0]).renameTo(new File(resultPath));
//        }
//        File[] files = new File[fpaths.length];
//        for (int i = 0; i < fpaths.length; i++) {
//            files[i] = new File(fpaths[i]);
//            if (!files[i].exists() || !files[i].isFile()) {
//                return false;
//            }
//        }
//        File resultFile = new File(resultPath);
//        try {
//            FileChannel resultFileChannel = new FileOutputStream(resultFile, true).getChannel();
//            for (int i = 0; i < fpaths.length; i++) {
//                FileChannel blk = new FileInputStream(files[i]).getChannel();
//                resultFileChannel.transferFrom(blk, resultFileChannel.size(), blk.size());
//                blk.close();
//            }
//            resultFileChannel.close();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//            return false;
//        } catch (IOException e) {
//            e.printStackTrace();
//            return false;
//        }
//        for (int i = 0; i < fpaths.length; i++) {
//            files[i].delete();
//        }
//        return true;
//    }
    public static boolean mergeFilesNio(Integer total, String path, String resultPath) throws Exception {
        File resultFile = new File(resultPath);
        File[] files = new File[total];
        FileChannel resultFileChannel = new FileOutputStream(resultFile, true).getChannel();
        for (int i = 0; i < total; i++) {
            File file = new File(path + (i + 1));
            FileChannel blk = new FileInputStream(file).getChannel();
            resultFileChannel.transferFrom(blk, resultFileChannel.size(), blk.size());
            blk.close();
            files[i] = file;
        }
        resultFileChannel.close();
        for (int i = 0; i < files.length; i++) {
            files[i].delete();
        }
        return true;
    }

    public static boolean mergeFiles(String[] fpaths, String resultPath) {
        if (fpaths == null || fpaths.length < 1) {
            return false;
        }
        if (fpaths.length == 1) {
            return new File(fpaths[0]).renameTo(new File(resultPath));
        }
        File[] files = new File[fpaths.length];
        for (int i = 0; i < fpaths.length; i++) {
            files[i] = new File(fpaths[i]);
            if (!files[i].exists() || !files[i].isFile()) {
                return false;
            }
        }
        File resultFile = new File(resultPath);
        try {
            int bufSize = 1024;
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(resultFile));
            byte[] buffer = new byte[bufSize];

            for (int i = 0; i < fpaths.length; i++) {
                BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(files[i]));
                int readcount;
                while ((readcount = inputStream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, readcount);
                }
                inputStream.close();
            }
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        for (int i = 0; i < fpaths.length; i++) {
            files[i].delete();
        }
        return true;
    }
}
